package com.auto.learning.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.CommentBean;
import com.auto.learning.event.Handle;
import com.auto.learning.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

@SuppressLint("NewApi")
public class CommentAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<CommentBean> list;
	private Handle handle;
	DisplayImageOptions options;
	
	public CommentAdapter(Context context,ArrayList<CommentBean> list,Handle handle){
		this.context = context;
		this.list = list;
		this.handle = handle;
		initImageLoader();
	}
	

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getId();
	}

	ViewHolder holder = null;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final CommentBean bean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.comment_adapter2, null);
			holder = new ViewHolder();
			holder.fg_me_userpic = (CircleImageView) convertView.findViewById(R.id.fg_me_userpic);
			holder.tv_comment_number = (TextView) convertView.findViewById(R.id.tv_comment_number);
			holder.tv_username = (TextView) convertView.findViewById(R.id.tv_username);
			holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.linear_favor = (LinearLayout) convertView.findViewById(R.id.linear_favor);
			holder.tv_usergrade = (TextView) convertView.findViewById(R.id.tv_usergrade);
			holder.linear_re_info = (LinearLayout) convertView.findViewById(R.id.linear_re_info);
			holder.tv_recomment = (TextView) convertView.findViewById(R.id.tv_recomment);
			holder.tv_reusername = (TextView) convertView.findViewById(R.id.tv_reusername);
			holder.item_main_view = (LinearLayout) convertView.findViewById(R.id.item_main_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (bean.getIsSelf() == 1) {
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.text_comment_username_color));
			if (Build.VERSION.SDK_INT >= 16) {
				holder.tv_usergrade.setBackground(context.getResources().getDrawable(R.drawable.text_sharp_comment_other));
			}else {
				holder.tv_usergrade.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.text_sharp_comment_other));
			}
			holder.tv_usergrade.setTextColor(context.getResources().getColor(R.color.text_comment_username_color));
		}else if(bean.getIsSelf() == 2){
			holder.tv_username.setTextColor(context.getResources().getColor(R.color.main_green_color));
			if (Build.VERSION.SDK_INT >= 16) {
				holder.tv_usergrade.setBackground(context.getResources().getDrawable(R.drawable.text_sharp_comment_me));
			}else {
				holder.tv_usergrade.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.text_sharp_comment_me));
			}
			holder.tv_usergrade.setTextColor(context.getResources().getColor(R.color.main_green_color));
		}
		ImageLoader.getInstance().displayImage(bean.getFace(), holder.fg_me_userpic,options);
		holder.tv_comment_number.setText(bean.getFavourNum()+"");
		holder.tv_comment.setText(bean.getComment());
		holder.tv_time.setText(bean.getCreateTime());
		holder.tv_username.setText(bean.getName());
		holder.tv_usergrade.setText(bean.getGradeName()+"");
		holder.linear_favor.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(2, bean.getId()).sendToTarget();
			}
		});
		if (bean.getIsReply() == 2) {
			holder.linear_re_info.setVisibility(View.VISIBLE);
			holder.tv_recomment.setText(bean.getReplyComment());
			holder.tv_reusername.setText(bean.getReplyName());
		}else if (bean.getIsReply() == 1){
			holder.linear_re_info.setVisibility(View.GONE);
		}
		return convertView;
	}
	
	class ViewHolder{
		CircleImageView fg_me_userpic;
		TextView tv_comment_number,tv_username,tv_comment,tv_time,tv_usergrade,tv_reusername,tv_recomment;
		LinearLayout linear_favor,linear_re_info,item_main_view;
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_user)
		.showImageForEmptyUri(R.drawable.me_user)
		.showImageOnFail(R.drawable.me_user).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

}
